<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深入理解 Java 类加载机制</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .code-block {
            background: #282c34;
            color: #abb2bf;
            border-radius: 8px;
            padding: 1.5rem;
            overflow-x: auto;
            font-family: 'Consolas', 'Monaco', monospace;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        
        .code-block code {
            color: #e06c75;
        }
        
        .feature-icon {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        .section-divider {
            height: 2px;
            background: linear-gradient(to right, transparent, #667eea, transparent);
            margin: 3rem 0;
        }
        
        .first-letter::first-letter {
            font-size: 3.5rem;
            font-weight: 700;
            float: left;
            line-height: 1;
            margin-right: 0.5rem;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 1rem 1.5rem;
            border-radius: 8px;
            margin: 1rem 0;
            font-weight: 500;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6 text-center">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 animate-pulse">
                <i class="fas fa-code mr-4"></i>Java 类加载机制深度解析
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">
                探索 Class.forName() 与 ClassLoader 的奥秘
            </p>
            <div class="flex justify-center space-x-4">
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full">
                    <i class="fas fa-clock mr-2"></i>阅读时间：8分钟
                </span>
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full">
                    <i class="fas fa-tag mr-2"></i>Java 核心技术
                </span>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12 max-w-5xl">
        <!-- Introduction -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <p class="text-lg leading-relaxed text-gray-700 first-letter">
                在 Java 的世界里，类加载机制是一个既神秘又强大的特性。<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">Class.forName()</code> 和 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">ClassLoader</code> 作为两种主要的类加载方式，它们各有千秋，在不同的场景下发挥着重要作用。今天，让我们一起深入探索这两种机制的本质区别和应用场景。
            </p>
        </div>

        <!-- Visual Diagram -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-sitemap mr-3 feature-icon"></i>类加载机制概览
            </h2>
            <div class="mermaid">
                graph TD
                    A[Java 类加载机制] --> B[Class.forName]
                    A --> C[ClassLoader]
                    B --> D[加载类]
                    B --> E[初始化类]
                    B --> F[执行静态代码块]
                    C --> G[仅加载类]
                    C --> H[不初始化]
                    C --> I[自定义加载逻辑]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:3px,color:#fff
                    style B fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                    style C fill:#4facfe,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Class.forName() Section -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <h2 class="text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-magic mr-3 feature-icon"></i>1. Class.forName() 方法详解
            </h2>
            
            <p class="text-lg leading-relaxed text-gray-700 mb-6">
                <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">Class.forName()</code> 是一个静态方法，它不仅能够动态加载类，还会触发类的初始化过程。这使得它在某些场景下特别有用。
            </p>

            <div class="grid md:grid-cols-2 gap-6 mb-6">
                <div class="bg-gradient-to-br from-purple-50 to-pink-50 p-6 rounded-xl">
                    <h3 class="text-xl font-semibold mb-3 text-purple-800">
                        <i class="fas fa-star mr-2"></i>核心特性
                    </h3>
                    <ul class="space-y-2 text-gray-700">
                        <li><i class="fas fa-check-circle text-green-500 mr-2"></i>自动执行静态代码块</li>
                        <li><i class="fas fa-check-circle text-green-500 mr-2"></i>初始化静态变量</li>
                        <li><i class="fas fa-check-circle text-green-500 mr-2"></i>返回 Class<?> 对象</li>
                        <li><i class="fas fa-check-circle text-green-500 mr-2"></i>使用默认类加载器</li>
                    </ul>
                </div>
                
                <div class="bg-gradient-to-br from-blue-50 to-indigo-50 p-6 rounded-xl">
                    <h3 class="text-xl font-semibold mb-3 text-blue-800">
                        <i class="fas fa-code mr-2"></i>典型应用
                    </h3>
                    <ul class="space-y-2 text-gray-700">
                        <li><i class="fas fa-database text-blue-500 mr-2"></i>JDBC 驱动加载</li>
                        <li><i class="fas fa-plug text-blue-500 mr-2"></i>插件系统初始化</li>
                        <li><i class="fas fa-cogs text-blue-500 mr-2"></i>框架组件注册</li>
                        <li><i class="fas fa-puzzle-piece text-blue-500 mr-2"></i>反射机制应用</li>
                    </ul>
                </div>
            </div>

            <div class="highlight-box">
                <i class="fas fa-lightbulb mr-2"></i>
                重要提示：Class.forName() 会立即触发类的初始化，这在需要执行静态注册逻辑的场景中非常有用！
            </div>

            <h3 class="text-xl font-semibold mb-3 mt-6 text-gray-800">代码示例</h3>
            <div class="code-block">
                <pre>// 加载并初始化 MySQL 驱动
Class<?> driverClass = Class.forName("com.mysql.cj.jdbc.Driver");

// 此时驱动的静态代码块已经执行
// 驱动已经注册到 DriverManager</pre>
            </div>
        </div>

        <!-- ClassLoader Section -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <h2 class="text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-layer-group mr-3 feature-icon"></i>2. ClassLoader 深入剖析
            </h2>
            
            <p class="text-lg leading-relaxed text-gray-700 mb-6">
                <code class="bg-blue-100 text-blue-800 px-2 py-1 rounded">ClassLoader</code> 是 Java 中更底层、更灵活的类加载机制。它提供了对类加载过程的精细控制，是构建复杂应用的基石。
            </p>

            <div class="bg-gradient-to-r from-blue-50 to-cyan-50 p-6 rounded-xl mb-6">
                <h3 class="text-xl font-semibold mb-4 text-blue-800">
                    <i class="fas fa-sitemap mr-2"></i>双亲委派模型
                </h3>
                <div class="mermaid">
                    graph TB
                        A[Bootstrap ClassLoader] --> B[Extension ClassLoader]
                        B --> C[Application ClassLoader]
                        C --> D[Custom ClassLoader]
                        
                        style A fill:#ff6b6b,stroke:#fff,stroke-width:2px,color:#fff
                        style B fill:#4ecdc4,stroke:#fff,stroke-width:2px,color:#fff
                        style C fill:#45b7d1,stroke:#fff,stroke-width:2px,color:#fff
                        style D fill:#96ceb4,stroke:#fff,stroke-width:2px,color:#fff
                </div>
            </div>

            <div class="grid md:grid-cols-3 gap-4 mb-6">
                <div class="bg-gradient-to-br from-green-50 to-emerald-50 p-4 rounded-lg text-center">
                    <i class="fas fa-shield-alt text-4xl text-green-600 mb-2"></i>
                    <h4 class="font-semibold text-green-800">安全性保障</h4>
                    <p class="text-sm text-gray-600 mt-2">防止核心类被篡改</p>
                </div>
                <div class="bg-gradient-to-br from-yellow-50 to-amber-50 p-4 rounded-lg text-center">
                    <i class="fas fa-sync-alt text-4xl text-yellow-600 mb-2"></i>
                    <h4 class="font-semibold text-yellow-800">延迟初始化</h4>
                    <p class="text-sm text-gray-600 mt-2">按需加载，节省资源</p>
                </div>
                <div class="bg-gradient-to-br from-purple-50 to-pink-50 p-4 rounded-lg text-center">
                    <i class="fas fa-tools text-4xl text-purple-600 mb-2"></i>
                    <h4 class="font-semibold text-purple-800">高度可定制</h4>
                    <p class="text-sm text-gray-600 mt-2">支持自定义加载逻辑</p>
                </div>
            </div>

            <h3 class="text-xl font-semibold mb-3 text-gray-800">代码示例</h3>
            <div class="code-block">
                <pre>// 获取类加载器
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

// 加载类但不初始化
Class<?> clazz = classLoader.loadClass("com.example.MyClass");

// 此时静态代码块尚未执行
// 需要显式创建实例才会初始化
Object instance = clazz.getDeclaredConstructor().newInstance();</pre>
            </div>
        </div>

        <!-- Comparison Section -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-balance-scale mr-3 feature-icon"></i>3. 核心差异对比
            </h2>
            
            <div class="overflow-x-auto">
                <table class="w-full text-left">
                    <thead>
                        <tr class="bg-gradient-to-r from-purple-500 to-pink-500 text-white">
                            <th class="p-4 rounded-tl-lg">特性</th>
                            <th class="p-4">Class.forName()</th>
                            <th class="p-4 rounded-tr-lg">Class